package com.endes.android.view.aplicacion;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.endes.android.R;
import com.endes.android.context.Aplicacion;
import com.endes.android.context.Preferencias;
import com.endes.android.model.service.ConglomeradoService;
import com.endes.android.model.service.EntrevistadoraService;
import com.endes.android.model.service.SampselService;
import com.endes.android.model.service.SupervisoraService;
import com.endes.android.view.aplicacion.componentes.ArchivoEndes;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ImportacionArchivosActivity 
//extends EndesActivity 
extends Activity
implements View.OnClickListener
{
		
	private Button btnBackup;
	private Button btnCargarSampsel;
	private Button btnCargarInterv;
	private Button btnCargarSuperv;
	private Button btnCargarCluster;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        // Display a indeterminate progress bar on title bar
        //requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
 
        this.setContentView(R.layout.app_importacion_archivos);
        
        
        btnBackup = (Button) findViewById(R.id.btnBackup);
        btnBackup.setOnClickListener(this); 
                
        btnCargarSampsel = (Button) findViewById(R.id.btnCargarSampsel);
        btnCargarSampsel.setOnClickListener(this); 

        btnCargarInterv = (Button) findViewById(R.id.btnCargarInterv);
        btnCargarInterv.setOnClickListener(this); 
        
        btnCargarSuperv = (Button) findViewById(R.id.btnCargarSuperv);        
        btnCargarSuperv.setOnClickListener(this); 
        
        btnCargarCluster = (Button) findViewById(R.id.btnCargarCluster);
        btnCargarCluster.setOnClickListener(this); 
    }

	@Override
	public void onClick(View v) {
		
		ArchivoEndes arch = null; 
		
		try {
			
			switch (v.getId()) {		
			
			case R.id.btnBackup:
				//El Backup se crea luego del SWITCH
				//Aplicacion.getInstance().getAppContext().crearBackupEndes(this);
				
				break;
			case R.id.btnCargarSampsel:		{
				
				SampselService service = Aplicacion.getInstance().getAppContext().sampselService;						   
			    	
		    	arch = new ArchivoEndes("sampsel.csv");		    	
		    	BufferedReader reader = arch.getReader();
				
		        String line;
		        line = reader.readLine();//descartando primera linea  
		        
		        service.deleteAll();
		        
		        int cong = Preferencias.getCodigoConglomerado(this);
		        
		        if(cong==0) {
			        while ((line = reader.readLine()) != null) {
			             String[] data = line.split(",");		             
			             service.save(data);
			        }   
		        }
		        else {
		        	//Graba solamente el sampsel del conglomerado indicado en las preferencias
		        	while ((line = reader.readLine()) != null) {		        		
			            String[] data = line.split(",");
			            service.saveByConglomerado(data, cong);			            
			        }
		        }
		        
		        break;
			}
			
			case R.id.btnCargarInterv:	 {	    
				EntrevistadoraService service = Aplicacion.getInstance().getAppContext().entrevistadoraService;						   
		    	
		    	arch = new ArchivoEndes("interv.csv");		    	
		    	BufferedReader reader = arch.getReader();
				
		        String line;
		        line = reader.readLine();//descartando primera linea  
		        
		        service.deleteAll();
		        
		        while ((line = reader.readLine()) != null) {
		             String[] data = line.split(",");		             
		             service.save(data);
		        }
		        
		        break;
			}
			case R.id.btnCargarSuperv:	 {	    
				SupervisoraService service = Aplicacion.getInstance().getAppContext().supervisoraService;						   
		    	
		    	arch = new ArchivoEndes("supervis.csv");		    	
		    	BufferedReader reader = arch.getReader();
				
		        String line;
		        line = reader.readLine();//descartando primera linea  
		        
		        service.deleteAll();
		        
		        while ((line = reader.readLine()) != null) {
		             String[] data = line.split(",");		             
		             service.save(data);
		        }
		        
		        break;
			}
			
			case R.id.btnCargarCluster:	 {	    
				ConglomeradoService service = Aplicacion.getInstance().getAppContext().conglomeradoService;						   
		    	
		    	arch = new ArchivoEndes("clusters.csv");    	
		    	BufferedReader reader = arch.getReader();
				
		        String line;
		        line = reader.readLine();//descartando primera linea  
		        
		        service.deleteAll();
		        
		        while ((line = reader.readLine()) != null) {
		             String[] data = line.split(",");		             
		             service.save(data);
		        }
		        
		        break;
			}
			
			default:
				break;
			}	
			
			//CREANDO BACKUP
			Aplicacion.getInstance().getAppContext().crearBackupEndes(this);
		
		} catch(FileNotFoundException fe) {
			Toast.makeText(this, "Archivo no encontrado: "+fe.getMessage(), Toast.LENGTH_LONG).show();
		} catch (Exception e) {
	    	Aplicacion.log(e);
		}		    
	    finally {
	        try {
	        	if(arch!=null)
	        		arch.cerrar();
	        }
	        catch (IOException e) {
	        	Aplicacion.log(e);
	        }
	    }
		
	}
	


}